package cl.pelu.operacion;

import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import cl.pelu.model.bo.dvo.ClienteDVO;
import cl.pelu.model.bo.dvo.ServicioBO;
import cl.pelu.model.bo.dvo.ServicioDVO;
import cl.pelu.model.bo.dvo.TablaDVO;
import cl.pelu.utils.PeluFacade;


public class MantenedorClienteForm  extends javax.swing.JInternalFrame {

	private static MantenedorClienteForm myInstance;
	
	
	private javax.swing.JLayeredPane mantenedorServicioLayeredPane;
	private javax.swing.JLayeredPane busquedaLayeredPane ;
	
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable clienteTable;
	
	private javax.swing.JLabel nombreLabel;
	private javax.swing.JLabel direccionLabel;
	private javax.swing.JLabel telefonoLabel;
	private javax.swing.JLabel busquedaClienteLabel   ;
	private javax.swing.JLabel rutLabel   ;
	private javax.swing.JLabel correoLabel   ;
	
	private javax.swing.JTextField busquedaClienteField;
	private javax.swing.JTextField rutField;
	private javax.swing.JTextField nombreField;
	private javax.swing.JTextField direccionField;
	private javax.swing.JTextField telefonoField;
	private javax.swing.JTextField correoField;
	
	
	
	private javax.swing.JButton buscarButton;
	private javax.swing.JButton actualizarButton;
	private javax.swing.JButton nuevoButton;
	private javax.swing.JButton guardarButton;
	private javax.swing.JButton eliminarButton;
	
	
	
	
	private SimpleDateFormat df ;
	
	
	
	public MantenedorClienteForm() {
		super("Mantenedor Clientes");
		initComponents();
	}

	private void initComponents() {
		
		

		jScrollPane1 = new javax.swing.JScrollPane();
		clienteTable = new javax.swing.JTable();
		mantenedorServicioLayeredPane = new javax.swing.JLayeredPane();
		busquedaLayeredPane = new javax.swing.JLayeredPane();
		
		
		
		busquedaClienteLabel    = new javax.swing.JLabel("Busqueda de Cliente");
		rutLabel    = new javax.swing.JLabel("Rut");
		nombreLabel = new javax.swing.JLabel("Nombre");
		direccionLabel = new javax.swing.JLabel("Direccion");
		telefonoLabel    = new javax.swing.JLabel("Telefono");
		correoLabel    = new javax.swing.JLabel("Correo");
		
		
		
		busquedaClienteField = new javax.swing.JTextField();
		rutField = new javax.swing.JTextField();
		nombreField = new javax.swing.JTextField();
		direccionField = new javax.swing.JTextField();
		telefonoField = new javax.swing.JTextField();
		correoField = new javax.swing.JTextField();
		
		
		buscarButton  = new javax.swing.JButton("Buscar");
		actualizarButton = new javax.swing.JButton("Actualizar");
		nuevoButton = new javax.swing.JButton("Nuevo");
		guardarButton = new javax.swing.JButton("Guardar");
		eliminarButton = new javax.swing.JButton("Eliminar");
		
		busquedaClienteLabel.setBounds(200, 1, 200, 20);
		rutLabel 		.setBounds(30, 30, 200, 20);
		nombreLabel .setBounds(30, 70, 200, 20);
		direccionLabel	.setBounds(30, 110, 200, 20);
		telefonoLabel	.setBounds(30, 150, 200, 20);
		correoLabel	.setBounds(30, 190, 200, 20);
		
		
		rutField			.setBounds(180, 30, 200, 20);
		nombreField .setBounds(180, 70, 200, 20);
		direccionField			.setBounds(180, 110, 200, 20);
		telefonoField	.setBounds(180, 150, 200, 20);
		correoField	.setBounds(180, 190, 200, 20);
		
		
		busquedaClienteField.setBounds(400, 1, 300	, 20);
		
		
		
		guardarButton.setBounds(500,150 , 100, 20);
		eliminarButton.setBounds(850, 150, 100, 20);
		actualizarButton.setBounds(650,150 , 150, 20);
		
		buscarButton.setBounds(750,1 , 100, 20);
		nuevoButton.setBounds(900,1 , 100, 20);
		
		
		

		rutField.setEditable(false);
		actualizarButton.setEnabled(false);
		eliminarButton.setEnabled(false);
		guardarButton.setEnabled(false);
		
		busquedaClienteField.setToolTipText("Puede relizar la busqueda por rut o por nombre");
		
		jScrollPane1.setViewportView(clienteTable);
		mantenedorServicioLayeredPane.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(),"Detalle de Servicios"));
		mantenedorServicioLayeredPane.add(eliminarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(nombreField,	javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(direccionField,	javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(nombreLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(direccionLabel,	javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(guardarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(telefonoLabel,	javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(telefonoField,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(rutLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(rutField,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(actualizarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(correoLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(correoField,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		
		
		
		
		busquedaLayeredPane.add(busquedaClienteLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaLayeredPane.add(busquedaClienteField,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaLayeredPane.add(buscarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaLayeredPane.add(nuevoButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		
	
		
		setClosable(true);

		guardarButton .addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				
				limpiarTabla();
				try {
					guardarServicio();
					cargarTabla(new ClienteDVO());
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		eliminarButton .addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
		
				limpiarTabla();
				eliminarCliente();
				cargarTabla(new ClienteDVO());
				
			}
		});
		
		nuevoButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {

				actualizarButton.setEnabled(false);
				eliminarButton.setEnabled(false);
				guardarButton.setEnabled(true);
			
				rutField.setEnabled(true);
				rutField.setEditable(true);
				rutField.setText("");
				nombreField.setText("");
				direccionField.setText("");
				telefonoField.setText("");
				correoField.setText("");
				
				
			}
		});

		
		actualizarButton .addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
			
				limpiarTabla();
				try {
					actualizarCliente();
					cargarTabla(new ClienteDVO());
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		});
		
		buscarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
			
				ClienteDVO cliente = new ClienteDVO();
				
				if(!busquedaClienteField.getText().isEmpty()){
					cliente.setNombre(busquedaClienteField.getText());
					cliente.setRut(busquedaClienteField.getText());
				}
				
				
				
				limpiarTabla();
				cargarTabla(cliente);
				
			}
		});

		
		
		
		clienteTable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				TableMouseClicked(evt);
			}
		});
		
		
		
		clienteTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "Rut", "Nombre", "Direccion", "Felefono","Correo" }) {
			boolean[] canEdit = new boolean[] { false, false, false, false,false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		
			
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		
		
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addGap(17, 17, 17)
						.addComponent(busquedaLayeredPane,javax.swing.GroupLayout.PREFERRED_SIZE, 50,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(27, 27, 27)
						.addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE, 200,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(27, 27, 27)
						.addComponent(mantenedorServicioLayeredPane,javax.swing.GroupLayout.PREFERRED_SIZE, 300,javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(99, Short.MAX_VALUE)));
		
		
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(49, 49, 49)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(busquedaLayeredPane,javax.swing.GroupLayout.PREFERRED_SIZE, 1000,javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(mantenedorServicioLayeredPane
														,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														1000,
														javax.swing.GroupLayout.PREFERRED_SIZE)
														
												.addComponent(
														jScrollPane1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														1000,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap(134, Short.MAX_VALUE)));
		

		cargarTabla(new ClienteDVO()) ;
		pack();
	}
	
	
	public static MantenedorClienteForm getInstance() throws PropertyVetoException {
		if (myInstance == null) {
			myInstance = new MantenedorClienteForm();
		}
		return myInstance;
	}
	
	
	
	
	
	
	
	
	
	private void guardarServicio() throws SQLException
	{
		ClienteDVO cliente = new ClienteDVO();
		cliente.setRut(rutField.getText());
		cliente.setNombre(nombreField.getText());
		cliente.setDireccion(direccionField.getText());
		cliente.setTelefono(telefonoField.getText());
		cliente.setCorreo(correoField.getText());
		
		PeluFacade.getPeluqueriaComponent().insertCliente(cliente); 
	}
	
	
	private void actualizarCliente() throws SQLException{
	
		ClienteDVO cliente = new ClienteDVO();
		cliente.setRut(rutField.getText());
		cliente.setNombre(nombreField.getText());
		cliente.setDireccion(direccionField.getText());
		cliente.setTelefono(telefonoField.getText());
		cliente.setCorreo(correoField.getText());
		
		PeluFacade.getPeluqueriaComponent().updateClienteByRut(cliente);
	}
		
	
	private void eliminarCliente(){
		
		
		try {
			PeluFacade.getPeluqueriaComponent().eliminarClientesByRut(rutField.getText());
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), e.toString(), JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
	private void TableMouseClicked(java.awt.event.MouseEvent evt) {

		ClienteDVO cliente = new ClienteDVO();
		cliente.setRut(""+clienteTable.getValueAt(clienteTable.getSelectedRow(), 0));
		cliente.setNombre(""+clienteTable.getValueAt(clienteTable.getSelectedRow(), 1));
		cliente.setDireccion(""+clienteTable.getValueAt(clienteTable.getSelectedRow(), 2));
		cliente.setTelefono(""+clienteTable.getValueAt(clienteTable.getSelectedRow(), 3));
		cliente.setCorreo(""+clienteTable.getValueAt(clienteTable.getSelectedRow(), 4));
		
		
		actualizarButton.setEnabled(true);
		eliminarButton.setEnabled(true);
		guardarButton.setEnabled(false);
		rutField.setEnabled(false);
				
		rutField.setText(cliente.getRut());
		nombreField.setText(cliente.getNombre());
		direccionField.setText(cliente.getDireccion());
		telefonoField.setText(cliente.getTelefono());
		correoField.setText(cliente.getCorreo());
		
		
		
		
	}

	
	
	void limpiarTabla (){
		
		DefaultTableModel tableModel = (DefaultTableModel) this.clienteTable.getModel();
		
		int a = tableModel.getRowCount();
		
		for (int i = 0; i < a; i++) {
			tableModel.removeRow(0);
			
		}
		
	}
	
	private void cargarTabla(ClienteDVO cliente) 
	{
		

		
		try {
				ArrayList <ClienteDVO>  clientesDVO= PeluFacade.getPeluqueriaComponent().getClientes(cliente);
				DefaultTableModel tableModel = (DefaultTableModel) this.clienteTable.getModel();
				for (ClienteDVO clienteDVO : clientesDVO) {
				
						
								tableModel.addRow(new Object[] { 
										clienteDVO .getRut(),
										clienteDVO .getNombre(),
										clienteDVO .getDireccion(),
										clienteDVO .getTelefono(),
										clienteDVO .getCorreo()
									});
				}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void limpiarFormulario(){
		
		rutField.setEditable(false);
		actualizarButton.setEnabled(false);
		eliminarButton.setEnabled(false);
		guardarButton.setEnabled(false);
		limpiarTabla();
		
	}

}